package cn.zbh.basic.controller;

import cn.zbh.basic.exception.BusinessException;
import cn.zbh.basic.service.IVerifyCodeService;
import cn.zbh.basic.util.AjaxResult;
import cn.zbh.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferStrategy;
import java.util.Map;

/**
 * 图图形验证码
 */
@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {

   @Autowired
   private IVerifyCodeService iVerifyCodeService;
   @Autowired
   private IUserService iUserService;

      @GetMapping("/image/{key}")
         public AjaxResult verifyCode(@PathVariable(value = "key") String key){
         try {
            String baseStr = iVerifyCodeService.verifyCode(key);
            //把返回的数据封装到AjaxResult，返回给页面
            return AjaxResult.me().setResultObj(baseStr);
         } catch (BusinessException e) {

            return AjaxResult.me().setMsg(e.getMessage());
         }catch (Exception e) {

            return AjaxResult.me().setMsg("系统繁忙");
         }
      }

   /**
    * phone.imageCode.base64ImageCode
    * 手机验证码
    * @return
    */
   @PostMapping("/smsCode")
      public AjaxResult smsCode(@RequestBody Map<String,String> map) {

      try {
         iVerifyCodeService.smsCode(map);
         return AjaxResult.me();
      } catch (BusinessException e) {
         e.printStackTrace();
         return AjaxResult.me().setMsg(e.getMessage());
      }catch (Exception e) {
         e.printStackTrace();
         return AjaxResult.me().setMsg("系统繁忙");
      }
   }

   @PostMapping("/binderSmsCode")
   public AjaxResult binderSmsCode(@RequestBody Map<String,String> map) {

      try {
         iVerifyCodeService.binderSmsCode(map);
         return AjaxResult.me();
      } catch (BusinessException e) {
         e.printStackTrace();
         return AjaxResult.me().setMsg(e.getMessage());
      }catch (Exception e) {
         e.printStackTrace();
         return AjaxResult.me().setMsg("系统繁忙");
      }
   }
}
